package course.filter;

import course.pojo.entity.Student;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author HLF
 * @date 2022/7/5 15:04
 */
@WebFilter(filterName = "StudentLoginFilter",value = {
        "/student/stuTeaController/*",
        "/student/studentOwnController/*",
        "/stu_center_update.jsp",
        "/student_center.jsp",
        "/stusub_manage.jsp",
        "/stutea_manage.jsp",
})
public class StudentLoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        HttpSession session = req.getSession();
        Student student = (Student) session.getAttribute("sstudent");
        if (student == null) {
            req.setAttribute("roleMsg", "未登录或学生权限不足，无法访问！");
            req.getRequestDispatcher("/422.jsp").forward(req, resp);
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
